/*
 * 文  件   名: CategoryWidget.java
 * 版         本 : docrepository-console(Anttribe). All rights reserved.
 * 描         述 : <描述>
 * 修   改  人: zhaoyong
 * 修改时间: 2014年10月7日
 */
package org.anttribe.docrepository.console.category.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.anttribe.docrepository.category.domain.Category;
import org.anttribe.docrepository.category.manager.CategoryManager;
import org.anttribe.docrepository.common.UnifyException;
import org.anttribe.docrepository.console.category.vo.CategoryVo;
import org.anttribe.saturn.runtime.stereotype.Widget;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 目录分类管理
 * 
 * @author zhaoyong
 * @version 2014年10月7日
 */
@Widget(name = "CategoryWidget")
@RequestMapping("/category")
public class CategoryWidget
{
    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryWidget.class);
    
    /**
     * categoryManager
     */
    @Autowired
    private CategoryManager categoryManager;
    
    /**
     * 获取分类数据列表
     * 
     * @param request HttpServletRequest
     * @return ModelAndView
     */
    @RequestMapping("/category-list")
    public ModelAndView doCategoryList(HttpServletRequest request)
    {
        ModelAndView mv = new ModelAndView();
        
        List<Category> categoryList = new ArrayList<Category>();
        try
        {
            categoryList = categoryManager.getCategoryByPagination(0, 10);
        }
        catch (UnifyException e)
        {
            LOGGER.warn("While getting category List, get error: {}", e.getErrorNo());
        }
        
        mv.addObject("categoryList", categoryList);
        mv.setViewName("category/category-list");
        
        return mv;
    }
    
    /**
     * 目录添加/编辑
     * 
     * @param request HttpServletRequest
     * @return ModelAndView
     */
    @RequestMapping("/category-edit")
    public ModelAndView categoryEditIndex(HttpServletRequest request)
    {
        ModelAndView mv = new ModelAndView();
        
        // 从请求中获取categoryId, 如果能获取到数据，为编辑
        String categoryId = request.getParameter("categoryId");
        if (!StringUtils.isEmpty(categoryId))
        {
            // 根据id获取目录的数据
            Category category = null;
            mv.addObject("category", category);
        }
        
        mv.setViewName("category/category-edit");
        return mv;
    }
    
    /**
     * 处理目录的edit
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param categoryVo CategoryVo
     * @return ModelAndView
     */
    @RequestMapping("/doCategoryEdit")
    public ModelAndView doCategoryEdit(HttpServletRequest request, HttpServletResponse response,
        @ModelAttribute CategoryVo categoryVo)
    {
        ModelAndView mv = new ModelAndView();
        
        if (null != categoryVo)
        {
            Category category = (Category)categoryVo;
            try
            {
                categoryManager.saveOrUpdateCategory(category);
                mv.setViewName("category");
            }
            catch (UnifyException e)
            {
                LOGGER.warn("While saving or updating category, get error: {}", e.getErrorNo());
                mv.addObject("errorNo", e.getErrorNo());
                mv.setViewName("/category/edit");
            }
        }
        return mv;
    }
}